/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.eclipse.wst.validation.internal.core.ValidationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.WorkbenchTasks;


/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class AppenderContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-validation-appender.xml");
		// remove all markers on document
		WorkbenchTasks.deleteAllMarkersOnFile(file);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		validator = new ContentValidator();
		validator.connect(document);
	}

	@After
	public void tearDown() throws Exception {
		validator.disconnect(document);
		super.tearDownProject();
	}
	
	@Test 
	public void testEmptyInheritedMethods() throws ValidationException {
		validator.cleanup(null);
		validator.validate(null, null);
	}
	
	@Test
	public void testScanAndValidateEndOfAppenderTag() throws Exception {
		int offset = getDocumentOffset(5, 7, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}
	
	
	@Test
	public void testScanAndValidateValidAppenderClass() throws Exception {
		int offset = getDocumentOffset(4, 45, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateInvalidAppenderClass() throws Exception {
		int offset = getDocumentOffset(7, 43, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateUnknownAppenderClass() throws Exception {
		int offset = getDocumentOffset(10, 43, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateMissingAppenderClass() throws Exception {
		int offset = getDocumentOffset(13, 29, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateNewInvalidAppenderClass() throws Exception {
		int offset = getDocumentOffset(4, 45, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.ConsoleAppender".length(), "unknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateNewEmptyAppenderClass() throws Exception {
		int offset = getDocumentOffset(4, 45, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.ConsoleAppender".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateFixInvalidAppenderClass() throws Exception {
		int offset = getDocumentOffset(10, 37, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownAppender".length(), "org.apache.log4j.ConsoleAppender");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepInvalidAppenderClass() throws Exception {
		int offset = getDocumentOffset(10, 37, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownAppender".length(), "ConsoleAppender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepInvalidAppenderClass2() throws Exception {
		int offset = getDocumentOffset(10, 37, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepInvalidAppenderClass3() throws Exception {
		int offset = getDocumentOffset(10, 37, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepValidAppenderClass() throws Exception {
		int offset = getDocumentOffset(4, 45, "appender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.ConsoleAppender".length(), "org.apache.log4j.FileAppender");
		validator.validate(getDirtyRegion(offset), null, null); 		
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

}
